// 顶升自检

<template>
  <f7-page class="common-page page-tab has-bottom-btns">
    <f7-navbar>
      <f7-nav-left>
        <f7-link back icon-material="arrow_back" force></f7-link>
      </f7-nav-left>
      <f7-nav-title>顶升自检</f7-nav-title>
      <f7-nav-right></f7-nav-right>
    </f7-navbar>
    <div class="common-page-content">
      <v-accordion title="检查信息填写" class="infoList">
        <van-form input-align="right" ref="form" :readonly="!canEdit">
          <van-field
            v-model="models.acceptance.checkUserName"
            label="检查人"
            placeholder="检查人"
            required
            :rules="[{ required: true, message: '请填写检查人' }]"
          />
          <van-field
            label="检查结论"
            required
            name="radio"
            :rules="[{ required: true, message: '请填写检查结论' }]"
          >
            <template #input>
              <van-radio-group v-model="models.acceptance.isPass" direction="horizontal" :disabled="!canEdit">
                <van-radio :name="1">通过</van-radio>
                <van-radio :name="0">不通过</van-radio>
              </van-radio-group>
            </template>
          </van-field>
          <van-field
            v-model="models.acceptance.checkTime"
            label="检查时间"
            readonly 
            placeholder="检查时间"
            required
            clickable
            is-link
            :rules="[{ required: true, message: '请填写检查时间' }]"
            @click="canEdit?showCalendar = true:''"
          />
          <van-calendar v-model="showCalendar" :min-date="new Date(2018, 0, 1)" @confirm="onConfirm" color="#1990FF"/>
          <van-field
            v-model="models.acceptance.checkMemo"
            label="检查意见"
            type="textarea"
            rows="5"
            autosize
            placeholder="检查意见"
            required
            :rules="[{ required: true, message: '请填写检查意见' }]"
          />
        </van-form>
      </v-accordion>
      <v-picture-list
          v-if="models.model&&controlData.property"
          :props="{ name: 'customName' }"
          :disabled="!canEdit"
          listName="installList"
          title="顶升自检表"
          typeCode="EQM_UP_SELF_INSPECT"
          :dataKey="models.model.caseId"
          :attachCraneType="controlData.property.equipmentType"
          @onAfterQuery="(val)=>list$1=val"
          ref="installList"
          extensions="jpg,jpeg,png,docx,doc,pdf"
          accept=".jpg,.jpeg,.png,.docx,.doc,.pdf"
          :address="address"
      >
        <span 
          slot="nameRight" 
          style="color:#1990ff;font-size:13px;font-weight:400" 
          @click="downLoad"
        >下载模板</span>
      </v-picture-list>
    </div>
    <f7-toolbar position="bottom" no-shadow class="fff" v-if="canEdit">
      <f7-button fill class="submit-btn" @click="fnSubmit()">提交</f7-button>
    </f7-toolbar>
  </f7-page>
</template>

<script>
import Config from "@/libs/config";
import Controller from "@/libs/framework/controller";
export default class sInsp extends Controller {
  onBeforeInit() {
    this.urls = {
      model: "/equipment/eqmUpSelfCheck/getEntity",
      save: "/equipment/eqmUpSelfCheck/save"
    };
  }
  onInit(){}
  onBeforeQuery(type, conditions, params) {
    this.vm.$f7.preloader.show();
  }
  onAfterQuery(type, isSuccess, result) {
    if(!isSuccess){
      setTimeout(() => {
        this.vm.$f7.preloader.hide();
        this.vm.$f7.dialog.alert('数据加载失败，请刷新页面重试')
      }, 3000);
    }
    if(!result.models.acceptance.checkTime) {
      result.models.acceptance.checkTime = this.vm.$formatter.formatDate(new Date(), 'yyyy-MM-dd')
    }else{
      result.models.acceptance.checkTime = this.vm.$formatter.formatDate(result.models.acceptance.checkTime, 'yyyy-MM-dd')
    }
    this.vm.$f7.preloader.hide();
    return result;
  }

  onAfterSave (isSuccess, result) {
      if (isSuccess) {
        this.$f7.views.current.router.back({force:true})
      }
  }

  mixin() {
    return {
      props: {},
      data() {
        return {
          models:{
            acceptance:{},
            model:{}
          },
          showCalendar:false,
          list$1:[],
          address:'',
        };
      },
      computed: {
        canEdit(){
          return this.models.model.pStatus!=7&&this.models.model.pstatus!=999&&this.controlData.isInsOrg
        },
      },
      created () {
        this.loadLocation()
      },
      methods: {
        downLoad(){
          let url=`${Config.server}/admin/attachmentFormDetail/downloadSelfInspectTemplate/${this.models.acceptance.notifyId}/UP_INSPECT`
          if(this.$util.isUmSdk()){
            url+='.docx'
            umSdk.scanFile({
                downloadurl: url, // 文件预览网络地址
                filename: '自检表.docx',
                success: function (val) {
                  
                },
                fail: function (err) { 
                  
                }
            })
          }else{
            window.location.href = url;
          }
        },
        //获取地址
        loadLocation() {
            let self = this
            self.$util.getCurrentPosition(
                (pos) => {
                    if (pos.addresses) {
                        self.address = pos.addresses
                    }else if(pos.data) {
                        self.address = pos.data.name
                    }
                },
                (error) => {
                    // self.$f7.dialog.toast(
                    //     '获取定位失败,请确认是否授权定位功能'
                    // )
                }
            )
        },
        onConfirm(date) {
          this.models.acceptance.checkTime = this.$formatter.formatDate(date, 'yyyy-MM-dd');
          this.showCalendar = false;
        },
        // getFileFlag(list,ref){
        //   return this[list].every((item,index)=>{
        //     return item.isMustUpload&&this.$refs[ref].$refs[`${ref}${index+1}`][index].getUploadedCount()>0
        //   })
        // },
        async getFileFlag(list,ref){
          let func=()=>{
            return new Promise((resolve, reject) => {
              let num=0
              if(this[list].length>0){
                this[list].forEach((item,index)=>{
                  if(item.isMustUpload&&this.$refs[ref].$refs[`${ref}${index+1}`][index].getUploadedCount()<1) {
                    num++
                  }
                })
              }
              resolve(num)
            })
          }
          let num=await func()
          if(num>0) {
            return false
          }else{
            return true
          }
        },
        fnSubmit(){
          this.$refs.form.validate().then(async (_) => {
            if(!await this.getFileFlag('list$1','installList')) return this.$toast('请上传顶升自检表！')
            this.save(this.models)
          })
          .catch((err) => {
              console.log(err);
              this.$toast('请填写必填项！')
          });
        },
      },
      mounted() {},
      watch: {},
    };
  }
}
</script>

<style lang="less" scoped>
@import url('../../../../assets/css/commonPage.less');
@mainColor:#1990FF;
.common-page{
  /deep/ .accordion{
    background: #fff;
    margin-top: 10px!important;
  }
  /deep/ .van-field{
    input.van-field__control{
      font-size: 14px;
      height: auto;
    }
  }
  /deep/ .van-cell{
    padding: 16px;
  }
  .pic_upload{
    padding: 10px;
  }
}
</style>
